package com.hundsun.yr.universal.p2p.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.hundsun.yr.universal.library.adapter.ViewCreator;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xx on 2017/7/10.
 */
public class MoreItemHolderAdapter<E> extends BaseAdapter {
    private List<E> mDataCache;
    private int position;
    private LayoutInflater mInflater;
    private ViewCreator<E> mCreator;
    private int mViewTypeCount;
    private int mItemViewType;

    public MoreItemHolderAdapter(Context mContext, ViewCreator<E> creator) {
        this(mContext, creator, (List) null);
    }

    public MoreItemHolderAdapter(Context mContext, ViewCreator<E> creator, List<E> data) {
        this.mViewTypeCount = 1;
        this.mCreator = creator;
        this.mInflater = LayoutInflater.from(mContext);
        this.mDataCache = data == null ? new ArrayList() : new ArrayList(data);
    }

    public void update(List<E> data) {
        this.mDataCache = data;
        this.notifyDataSetChanged();
    }

    public void add(List<E> set) {
        if (this.mDataCache == null) {
            this.mDataCache = new ArrayList();
        }

        this.mDataCache.addAll(set);
        this.notifyDataSetChanged();
    }

    public void add(E item) {
        if (this.mDataCache == null) {
            this.mDataCache = new ArrayList();
        }

        this.mDataCache.add(item);
        this.notifyDataSetChanged();
    }

    public int getCount() {
        return this.mDataCache == null ? 0 : this.mDataCache.size();
    }

    public E getItem(int position) {
        return this.mDataCache == null ? null : this.mDataCache.get(position);
    }

    public long getItemId(int position) {
        return (long) position;
    }

    public void setPosition(int position) {
        this.position = position;
        this.notifyDataSetChanged();
    }

    public int getPosition() {
        return this.position;
    }

    public void setViewTypeCount(int viewTypeCount) {
        this.mViewTypeCount = viewTypeCount;
    }

    public int getViewTypeCount() {
        return this.mViewTypeCount;
    }

    public void setItemViewType(int itemViewType) {
        this.mItemViewType = itemViewType;
    }

    public int getItemViewType(int position) {
        return this.mItemViewType;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        MoreItemHolderAdapter.ViewHolder holder = null;
        if (convertView == null) {
            holder = new MoreItemHolderAdapter.ViewHolder();
            convertView = this.mCreator.createView(this.mInflater, parent, position, this.getItem(position));
            holder.view = convertView;
            if (convertView != null) {
                convertView.setTag(holder);
            }
        } else {
            holder = (MoreItemHolderAdapter.ViewHolder) convertView.getTag();
        }

        this.mCreator.updateView(holder.view, position, this.getItem(position));
        return convertView;
    }

    public void clear() {
        this.mDataCache.clear();
        this.notifyDataSetChanged();
    }

    public void add(int index, E item) {
        if (this.mDataCache == null) {
            this.mDataCache = new ArrayList();
        }

        this.mDataCache.add(index, item);
        this.notifyDataSetChanged();
    }

    public List<E> getAdapterList() {
        return this.mDataCache;
    }

    public void set(E oldItem, E newItem) {
        this.set(this.mDataCache.indexOf(oldItem), newItem);
    }

    public void set(int index, E item) {
        this.mDataCache.set(index, item);
        this.notifyDataSetChanged();
    }

    public void remove(E item) {
        this.mDataCache.remove(item);
        this.notifyDataSetChanged();
    }

    public void removeAll(List<E> data) {
        this.mDataCache.removeAll(data);
        this.notifyDataSetChanged();
    }

    public int indexOfItem(E item) {
        return this.mDataCache.indexOf(item);
    }

    public void remove(int index) {
        this.mDataCache.remove(index);
        this.notifyDataSetChanged();
    }

    public boolean contains(E item) {
        return this.mDataCache.contains(item);
    }

    private static class ViewHolder {
        public View view;

        private ViewHolder() {
        }
    }
}
